/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditSubject.java
 *
 * Created on 31-05-2011, 01:29:45
 */
package UpdateForm;

import ConnectData.Config;
import ConnectData.getConnections;
import Management.AdminForm;
import UploadData.Database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author s0lo
 */
public class EditSubject extends javax.swing.JDialog {

    private String SubName;
    private int SubID;
    private int CourseID;
    private Vector vtCourse;
    private Vector vtSubjectDetail;
    private Vector vtCourse2;
    /** Creates new form AddSickness */
    AdminForm parent;

    public EditSubject(java.awt.Frame parent, boolean modal, String SubName, int subID) {
        super(parent, modal);
        initComponents();
       
        this.SubName = SubName;
        this.SubID = subID;
        txtSubName.setText(SubName);
        initCourse();
        this.parent = (AdminForm) parent;
        this.setVisible(true);
    }

    public void initCourse() {
        vtCourse = new Database().loadCourse("");
        if (vtCourse.size() != 0) {
            for (int l = 0; l < vtCourse.size(); l++) {
                String courname = ((Vector) vtCourse.get(l)).get(1).toString();
                
                vtSubjectDetail = new Database().loadSubjectDetail(" where SubID=" + SubID + " order by [SubID]");
                if (vtSubjectDetail.size() != 0) {
                    for (int i = 0; i < vtSubjectDetail.size(); i++) {
                        int courid = Integer.parseInt(((Vector) vtSubjectDetail.get(i)).get(1).toString());
                        vtCourse2 = new Database().loadCourse(" where CourseID=" + courid + " order by [CourseName]");
                        for (int j = 0; j < vtCourse2.size(); j++) {
                            String courName2 = ((Vector) vtCourse2.get(j)).get(1).toString();
                            if (courName2.toString().equals(courname)) {
                                cblCourse.add(courName2, true);
                                break;
                            }
                        }
                    }
                }
                if (!cblCourse.getItems().contains(courname.toString())) {
                    cblCourse.add(courname, false);
                }
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtSubName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        cblCourse = new creator.components.CheckBoxList();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        jPanel2.setBackground(new java.awt.Color(153, 255, 153));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 20));
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("           EDIT SUBJECT");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addContainerGap(123, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(204, 255, 204));
        jPanel3.setAutoscrolls(true);

        txtSubName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubNameActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(0, 0, 51));
        jLabel1.setText("Name : ");

        btnSave.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSave.setForeground(new java.awt.Color(0, 0, 51));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/save.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnClose.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnClose.setForeground(new java.awt.Color(0, 0, 51));
        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/close1.png"))); // NOI18N
        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setForeground(new java.awt.Color(0, 0, 51));
        jLabel2.setText("Course :");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(22, 22, 22)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cblCourse, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addGap(28, 28, 28)
                        .addComponent(btnClose))
                    .addComponent(txtSubName, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel2)
                    .addContainerGap(258, Short.MAX_VALUE)))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSubName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(cblCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnSave)))
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGap(53, 53, 53)
                    .addComponent(jLabel2)
                    .addContainerGap(103, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSubNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubNameActionPerformed
        // TODO add your handling code here:
        btnSaveActionPerformed(evt);
}//GEN-LAST:event_txtSubNameActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed

        boolean valid = validateInformation();
        if (valid) {
           String sn = txtSubName.getText();
            Connection cn = new Config().cn;
            PreparedStatement pst = null;
            try {
                String strdel = "delete from SubjectDetail where SubID=?";
                pst = cn.prepareCall(strdel);
                pst.setInt(1, SubID);
                pst.executeUpdate();
                Vector listCour = cblCourse.SelectedItems();
                int courID;
                int kq = -1;
                if (listCour.size() > 0) {
                    for (int i = 0; i < listCour.size(); i++) {
                        String CourName = listCour.get(i).toString();
                        Vector vtCour = new Database().loadCourse("where CourseName='" + CourName + "' order by [CourseName]");
                        if (vtCour.size() > 0) {
                            String courName;
                            for (int j = 0; j < vtCour.size(); j++) {
                                courName = ((Vector) vtCour.get(j)).get(1).toString();
                                if (CourName.equals(courName)) {
                                    courID = Integer.parseInt(((Vector) vtCour.get(j)).get(0).toString());

                                    String InsSubDe = "INSERT INTO SubjectDetail(SubID,CourseID) VALUES(?,?)";
                                    pst = cn.prepareCall(InsSubDe);
                                    pst.setInt(1, SubID);
                                    pst.setInt(2, courID);
                                    kq = pst.executeUpdate();
                                }
                            }
                        }
                    }
                }
                String strUpd = "Update Subject set SubName=? where SubID=?";
                pst = cn.prepareCall(strUpd);
                pst.setString(1, sn);
                pst.setInt(2, SubID);
                pst.executeUpdate();
                if (kq != -1) {
                    JOptionPane.showMessageDialog(this, "Update successful!");
                    parent.loadSubject();
                    this.dispose();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    getConnections.closeObject(pst);
                    getConnections.closeObject(cn);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

    /**
     * @param args the command line arguments
     */
   public boolean validateInformation() {
        SubName = txtSubName.getText();
        if (SubName.equals("")) {
            JOptionPane.showMessageDialog(this, "you must input Subject Name!");
            txtSubName.requestFocus();
            return false;
        }
        Vector vtCour = cblCourse.SelectedItems();
        if (vtCour.size() <= 0) {
            JOptionPane.showMessageDialog(this, "you must check Course!");
            cblCourse.requestFocus();
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private creator.components.CheckBoxList cblCourse;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtSubName;
    // End of variables declaration//GEN-END:variables
}
